home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / util / sys / InstallerNG.lha / InstallerNG / examples / math.installer < prev    next >
Text File  |  2000-03-22  |  4KB  |  177 lines

  1.  
  2. ; this needs the InstallerNG!
  3. (if (not @installer-ng-version)
  4.     (abort "This script needs the InstallerNG by Jens Tröger")
  5. )
  6.  
  7. ; lets go!
  8. (user expert)
  9. (set @proceed-button "Interesting... go on!"
  10.      @abort-button "Urgs"
  11. )
  12.  
  13. (message "\n\n\nWelcome to some recursive math...\n\n\n"
  14.          "Please have a look at the source to see how to\n"
  15.          "program recursive functions and how to use\n"
  16.          "local environments of procedures!"
  17.          "\n\nNote: send CTRL-F to the installer process,\n"
  18.          "if you want to stop interpretation"
  19. )
  20.  
  21. ; --------------------------------------------------------------------------------
  22. ; define a recursive procedure which calculates the faculty of
  23. ; a given argument. use the LET function to create a local environment
  24. ; note: for arguments greater than 12 there will be an overflow,
  25. ;       but this does not result in any runtime error
  26.  
  27. (procedure faculty a
  28.  
  29.   (set fac_number (+ 1 fac_number))
  30.  
  31.   (let (set f a)
  32.  
  33.        (if (= f 1)
  34.            1
  35.            (* f (faculty (- f 1)))
  36.        )
  37.   )
  38. )
  39.  
  40. ; --------------------------------------------------------------------------------
  41. ; the function "Ackerman" is a extremly recursive function, which only
  42. ; runs with local variables. thus here you must use the LET function
  43. ;
  44. ; the definition of the ackerman function is:
  45. ; a(0,y) = y+1
  46. ; a(x,0) = a(x-1,1)
  47. ; a(x,y) = a(x-1,a(x,y-1))
  48.  
  49. (procedure ackerman a b
  50.  
  51.   (set ack_number (+ 1 ack_number))
  52.  
  53.   (let (set x a y b)
  54.  
  55.        (if (= x 0)
  56.            (+ y 1)
  57.            (if (= y 0)
  58.                (ackerman (- x 1) 1)
  59.                (ackerman (- x 1) (ackerman x (- y 1)))
  60.            )
  61.        )
  62.   )
  63. )
  64.  
  65. ; --------------------------------------------------------------------------------
  66. ; this is the well known function "fibonacci"; it is formally defined
  67. ; as follows:
  68. ;
  69. ; fib(0) = 1
  70. ; fib(1) = 1
  71. ; fib(n) = fib(n-1) + fib(n-2)
  72.  
  73. (procedure fibonacci f
  74.  
  75.   (set fib_number (+ 1 fib_number))
  76.  
  77.   (let (set n f)
  78.  
  79.        (if (= n 0) 1
  80.                    (if (= n 1) 1
  81.                                (+ (fibonacci (- n 1)) (fibonacci (- n 2)))
  82.                    )
  83.        )
  84.   )
  85. )
  86.  
  87. ; --------------------------------------------------------------------------------
  88. ; a graph, which has n nodes and which has a reflexive, symmtrical edge relation,
  89. ; has (edge n) edges
  90.  
  91. (procedure edge n
  92.  
  93.   (set k 0
  94.        x (- n 1)
  95.   )
  96.  
  97.   (while x
  98.          (set k (+ k x)
  99.               x (- x 1)
  100.          )
  101.   )
  102.  
  103.   ; return value
  104.   (+ n (* k 2))
  105. )
  106.  
  107. ; --------------------------------------------------------------------------------
  108. ; ask for the values
  109.  
  110. (set fac 3
  111.      fib 5
  112. )
  113.  
  114. (swing
  115.  
  116.   (set fac (asknumber (prompt "Faculty of what ?")
  117.                       (help "enter the number you want to know the faculty of")
  118.                       (default fac)
  119.                       (range 1 12)
  120.            )
  121.   )
  122.  
  123.   (set fib (asknumber (prompt "Fibonacci of what ?")
  124.                       (help "enter the number you want to know the fibonacci number of")
  125.                       (default fib)
  126.                       (range 1 25)
  127.            )
  128.   )
  129.  
  130.   (message "Done? So lets start the evaluation...")
  131. )
  132.  
  133. ; --------------------------------------------------------------------------------
  134. ; calculate and print the faculty
  135.  
  136. (working (cat "Please be patient... this really could take a while\n"
  137.               "(depends on the given values....)\n\n"
  138.               "Send CTRL-F to break"
  139.          )
  140. )
  141.  
  142. (set start_time (database "time"))
  143. (set fac_number 0
  144.      fib_number 0
  145.      ack_number 0
  146. )
  147.  
  148. (complete 0)
  149. (set result_fac (faculty fac))
  150.  
  151. (complete 33)
  152. (set result_fib (fibonacci fib))
  153.  
  154. (complete 66)
  155. (set result_ack (ackerman 1 2))
  156.  
  157. (set end_time (database "time"))
  158.  
  159. (complete 100)
  160. (message "Faculty of " fac " = " result_fac "\n"
  161.          "Fibonacci of " fib " = " result_fib "\n"
  162.          "Ackerman(1,2) = " result_ack "\n\n"
  163.          "Took from " start_time " until " end_time "\n\n"
  164.          "\n----------\n"
  165.          "Function call statistics: \n"
  166.          "Faculty " fac_number " times\n"
  167.          "Fibonacci " fib_number " times\n"
  168.          "Ackerman " ack_number " times"
  169. )
  170.  
  171. ; --------------------------------------------------------------------------------
  172. ; avoid stupid welcome...   :)
  173.  
  174. (exit (quiet))
  175. (welcome)
  176.  
  177.